//
//  SellDetailViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/7/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SellDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "SellDetailTableViewCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "RecommendModel.h"
#import "discountDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface SellDetailViewController ()<UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>
// 底部
@property (nonatomic, retain) UIScrollView *backScrollView;
//
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headerView;

// 接收model的数组
@property (nonatomic, retain) NSMutableArray *tableViewArr;

// 计数label
@property (nonatomic, retain) UILabel *countLabel;
@end

@implementation SellDetailViewController

- (void)dealloc
{
    [_sendModel release];
    [_backScrollView release];
    [_tableView release];
    [_headerView release];
    [_tableViewArr release];
    [_countLabel release];
    [super dealloc];
}
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = YES;


}


//- (void)viewWillDisappear:(BOOL)animated
//{
//    self.navigationController.navigationBar.translucent = YES;
//}


- (void)viewDidLoad {
    [super viewDidLoad];
//     Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.navigationItem.title = @"商家详情";
    self.navigationController.navigationBar.translucent = NO;
    // 设置标题颜色
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    // 字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor brownColor],NSForegroundColorAttributeName,nil]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png" ] style:UIBarButtonItemStyleDone target:self action:@selector(leftBack:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(rightClick:)];
    
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49 - 64)];
    self.backScrollView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    [self.view addSubview:self.backScrollView];
    self.backScrollView.pagingEnabled = NO;
//    self.backScrollView.contentSize = CGSizeMake(0, HEIGHT * 2);
    [_backScrollView release];
    
    // 创建tableView
//    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.backScrollView addSubview:self.tableView];
    [_tableView release];
    
    // headerView
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 400 * HEIGHT / 667)];
    self.headerView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = self.headerView;
    [_headerView release];
    
    // 背景图片
    UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180 * HEIGHT /667)];
    [topImageView sd_setImageWithURL:[NSURL URLWithString:self.sendModel.cover_path]];
    [self.headerView addSubview:topImageView];
    [topImageView release];
    
    // logo
    UIImageView *logoImageVIew = [[UIImageView alloc] initWithFrame:CGRectMake(20 * WIDTH / 375, 165 * HEIGHT / 667, 90 * WIDTH / 375, 60 * HEIGHT / 667)];
    logoImageVIew.backgroundColor = [UIColor grayColor];
    [logoImageVIew sd_setImageWithURL:[NSURL URLWithString:self.sendModel.logo_path]];
    logoImageVIew.layer.masksToBounds = YES;
    logoImageVIew.layer.cornerRadius = 10;
    logoImageVIew.layer.borderWidth = 3;
    logoImageVIew.layer.borderColor = [[UIColor whiteColor] CGColor];
    [self.headerView addSubview:logoImageVIew];
    [logoImageVIew release];
    
    // 商家名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH / 375, 235 * HEIGHT / 667, 300 * WIDTH / 375, 30 * HEIGHT / 667)];
    nameLabel.text = self.sendModel.name;
    nameLabel.font = [UIFont systemFontOfSize:15];
    [self.headerView addSubview:nameLabel];
    [nameLabel release];
    
    // 商家地址
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH / 375, 270 * HEIGHT / 667, 300 * WIDTH / 375, 50 * HEIGHT / 667)];
    addressLabel.text = self.sendModel.address;
    addressLabel.numberOfLines = 0;
    addressLabel.font = [UIFont systemFontOfSize:14];
    addressLabel.textColor = [UIColor grayColor];
    [self.headerView addSubview:addressLabel];
    [addressLabel release];
    
    // 分割
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 * WIDTH / 375, 350 * HEIGHT / 667, 335 * WIDTH / 375, 1 * HEIGHT / 667)];
    label.backgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:223 / 255.0 alpha:1];
    [self.headerView addSubview:label];
    [label release];
    
    // 计数label
    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(140 * WIDTH / 375, 360 * HEIGHT / 667, 100 * WIDTH / 375, 30 * HEIGHT / 667)];
    self.countLabel.font = [UIFont systemFontOfSize:15];
    [self.headerView addSubview:self.countLabel];
    [_countLabel release];
    
    
    
    [self createData];
}

- (void)rightClick:(UIButton *)button
{
    
}

//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    SellDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[SellDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    RecommendModel *model = self.tableViewArr[indexPath.row];
    cell.titleLabel.text = model.title;
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:model.cover_path]];
    cell.collectLabel.text = [NSString stringWithFormat:@"%@  收藏", [model.collectors_count stringValue]];
    cell.priceLabel.text = [NSString stringWithFormat:@"$%@", [model.actual_price stringValue]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    discountDetailViewController *discountVC = [[discountDetailViewController alloc] init];
    discountVC.ID = (NSString *)[self.tableViewArr[indexPath.row] bId];
    [self.navigationController pushViewController:discountVC animated:YES];
    [discountVC release];
}

// 解析
- (void)createData
{
    self.tableViewArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://hunliji.com/v2/api/app/merchants/%@/works.json?page=1&per_page=5&kind=set_meal", self.sendModel.bId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *worksArr = dic[@"works"];
        for (NSDictionary *tempDic in worksArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.tableViewArr addObject:model];
            [model release];
        }
        self.tableView.frame = CGRectMake(0, 0, WIDTH, 100 * self.tableViewArr.count + (400 * HEIGHT / 667));
        self.backScrollView.contentSize = CGSizeMake(0, 100 * self.tableViewArr.count + (400 * HEIGHT / 667));
        self.countLabel.text = [NSString stringWithFormat:@"共%ld个套餐", (unsigned long)self.tableViewArr.count];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 导航控制器返回按钮点击方法
- (void)leftBack:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
